#include "touch2.h"

#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4

int x_d, y_d;

//宏定义
//设备文件
#define TOUCH_PATH "/dev/input/event0"

int fd;

int getInit()
{
    fd = open(TOUCH_PATH, O_RDONLY); //只读打开
    if (fd < 0)
    {
        perror("open fail");
        return 0;
    }
}

int GetDirection()
{
    // 1.打开触摸屏
    // lcd_init();
    int x_start = -1, y_start = -1; //坐标的初值
    int x_end = -1, y_end = -1;     //坐标终点

    struct input_event ev;
    while (1)
    {
        // 2.不停地从文件中读取数据
        read(fd, &ev, sizeof(struct input_event));

        // 3.解析数据
        if (ev.type == EV_ABS) //触摸事件
        {
            if (ev.code == ABS_X)
            {
                if (-1 == x_start) // x轴
                {
                    x_start = ev.value; //起点
                }
                x_end = ev.value; //终点
            }
            if (ev.code == ABS_Y) // y轴
            {
                if (-1 == y_start)
                {
                    y_start = ev.value;
                }
                y_end = ev.value; //终点
            }
            if (ev.code == ABS_PRESSURE && ev.value == 0)
            {
                if (x_start != -1 && y_start != -1)
                {
                    break;
                }
            }
        }

        if (ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0) //按键事件
        {
            if (x_start != -1 && y_start != -1)
            {
                break;
            }
        }

        if (abs(x_end - x_start) > (y_end - y_start))
        {
            if (x_end - x_start > 150)
            {
                return 4;
            }
            else if (x_end - x_start < -150)
            {
                return 3;
            }
        }
        if (abs(x_end - x_start) < (y_end - y_start))
        {
            if (y_end - y_start > 150)
            {
                return 2;
            }
            else if (y_end - y_start < -150)
            {
                return 1;
            }
        }

        x_d = x_end;
        y_d = y_end;
    }

    //打印坐标
    printf("%d , %d\n", x_end, y_start);
    if ((x_d > 909) && (y_d > 545)) //退出 x909 y545
    {

        printf("quit!\n");
        show_color();
        return main();
    }
}
